package tankgame01;


import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyPanel extends JPanel implements KeyListener, Runnable {
    private MyTank mt = null;
    Vector<EnemyTank> enemyTanks = new Vector<>();
    Vector<Bomb> bombs = new Vector<>();
    Vector<lastInfo> lastInfos = null;
    AePlayWave aePlayWave = new AePlayWave("src\\tankMusic.wav");
    int winOrLose = 0;//1胜利2失败
    int tankSize = 8;
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;
    Vector<Wall> walls = new Vector<>();

    public MyPanel(int choice) {
        int y = 400;
        for (int i = 0; i < 3; i++) {
            for (int l = 0; l < 3; l++) {
                int x = 125;
                for (int j = 0; j < 75; j++) {
                    Wall wall = new Wall(x, y);
                    walls.add(wall);
                    x += 10;
                }
                y += 10;
            }
            y += 100;
        }
        Tank.setWalls(walls);
        BulletShot.setWalls(walls);
        if (choice == 1) {
            mt = new MyTank(500, 600);
            lastInfos = Recorder.getStoreEnemyTanksAndNum();
            for (int i = 0; i < lastInfos.size(); i++) {
                EnemyTank enemyTank = new EnemyTank(lastInfos.get(i).getX(), lastInfos.get(i).getY());
                enemyTank.setDirection(lastInfos.get(i).getDirection());
                enemyTank.setEnemyTanks(enemyTanks);
                new Thread(enemyTank).start();
                enemyTanks.add(enemyTank);
                enemyTank.shot();
            }
        } else if (choice == 2) {
            mt = new MyTank(500, 600);
            for (int i = 0; i < tankSize; i++) {
//                EnemyTank enemyTank = new EnemyTank(150 * (1 + i), 0);
                EnemyTank enemyTank = new EnemyTank((int) (Math.random() * 900), (int) (Math.random() * 300));
                enemyTank.setDirection((int) (Math.random() * 4));
                enemyTank.setEnemyTanks(enemyTanks);
                new Thread(enemyTank).start();
                enemyTanks.add(enemyTank);
            }
        } else {
            System.out.println("你的输入有误请重新开始游戏");
        }
        Recorder.setEnemyTanks(enemyTanks);
        image1 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/bomb_3.gif"));
    }

    public void showInfo(Graphics g) {
        g.setColor(Color.black);
        Font font = new Font("宋体", Font.BOLD, 25);
        g.setFont(font);
        g.drawString("您累计击毁坦克数量为：", 1020, 30);
        drawTank(1020, 60, g, 0, 1);
        g.setColor(Color.black);
        g.drawString(Recorder.getEnemyTankNum() + "", 1080, 100);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        showInfo(g);
        if (mt.isLive) {
            drawTank(mt.getX(), mt.getY(), g, mt.getDirection(), 0);
        }
        for (int i = 0; i < mt.bulletShots.size(); i++) {
            BulletShot bulletShot = mt.bulletShots.get(i);
            if (bulletShot.isLive) {
                g.setColor(Color.CYAN);
                g.fillOval(bulletShot.getX(), bulletShot.getY(), 5, 5);
            } else {
                mt.bulletShots.remove(bulletShot);
            }
        }
        Image image = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/123.gif"));
//        g.drawImage(image, mt.getX(), mt.getY(), 40, 60, this);
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            if (enemyTank.isLive) {
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
//                enemyTank.random((int)(Math.random() * 4));
            }
            for (int j = 0; j < enemyTank.bulletShots.size(); j++) {
                BulletShot bulletShot = enemyTank.bulletShots.get(j);
                if (bulletShot.isLive) {
                    g.fillOval(bulletShot.getX(), bulletShot.getY(), 5, 5);
                } else {
                    enemyTank.bulletShots.remove(bulletShot);
                }
            }
        }
        for (Wall wall : walls) {
            drawWall(wall.getX(), wall.getY(), g);
        }
        for (int i = 0; i < bombs.size(); i++) {
            Bomb bomb = bombs.get(i);
            if (bomb.life > 6) {
                g.drawImage(image1, bomb.x, bomb.y, 60, 60, this);
            } else if (bomb.life > 3) {
                g.drawImage(image2, bomb.x, bomb.y, 60, 60, this);
            } else if (bomb.life > 0) {
                g.drawImage(image3, bomb.x, bomb.y, 60, 60, this);
            }
            bomb.lifeDown();
            if (!bomb.isLive) {
                bombs.remove(bomb);
            }
        }
    }

    public void drawWall(int x, int y, Graphics g) {
        g.setColor(Color.ORANGE);
        g.draw3DRect(x, y, 10, 10, false);
        g.setColor(Color.orange);
        g.fill3DRect(x, y, 9, 9, false);
    }

    public void drawTank(int x, int y, Graphics g, int direct, int type) {
        switch (type) {
            case 0://我方坦克
                g.setColor(Color.GREEN);
                break;
            case 1://敌方坦克
                g.setColor(Color.red);
                break;
        }
        //0向上，1向右，2向下，3向左
        switch (direct) {
            case 0:
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 19, y + 30, x + 19, y);
                g.drawLine(x + 20, y + 30, x + 20, y);
                g.drawLine(x + 21, y + 30, x + 21, y);
                break;
            case 1:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 30, y + 19, x + 60, y + 19);
                g.drawLine(x + 30, y + 20, x + 60, y + 20);
                g.drawLine(x + 30, y + 21, x + 60, y + 20);
                break;
            case 2:
                g.fill3DRect(x, y, 10, 60, false);
                g.fill3DRect(x + 30, y, 10, 60, false);
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 19, y + 30, x + 19, y + 60);
                g.drawLine(x + 20, y + 30, x + 20, y + 60);
                g.drawLine(x + 21, y + 30, x + 21, y + 60);
                break;
            case 3:
                g.fill3DRect(x, y, 60, 10, false);
                g.fill3DRect(x, y + 30, 60, 10, false);
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 30, y + 19, x, y + 19);
                g.drawLine(x + 30, y + 20, x, y + 20);
                g.drawLine(x + 30, y + 21, x, y + 20);
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
//      或者if (e.getKeyCode() == KeyEvent.VK_W) {}
        if ((char) e.getKeyCode() == 'W') {
            if (mt.getDirection() != 0) {
                mt.setDirection(0);
            }
            mt.moveUp();
        } else if ((char) e.getKeyCode() == 'S') {
            if (mt.getDirection() != 2) {
                mt.setDirection(2);
            }
            mt.moveDown();
        } else if ((char) e.getKeyCode() == 'D') {
            if (mt.getDirection() != 1) {
                mt.setDirection(1);
            }
            mt.moveRight();
        } else if ((char) e.getKeyCode() == 'A') {
            if (mt.getDirection() != 3) {
                mt.setDirection(3);
            }
            mt.moveLeft();
        }
        if (e.getKeyCode() == KeyEvent.VK_J) {
            mt.shot();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        boolean loop = true;
        while (loop) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                for (int j = 0; j < enemyTank.bulletShots.size(); j++) {
                    BulletShot bulletShot = enemyTank.bulletShots.get(j);
                    if (bulletShot.isLive) {
                        hitTank(bulletShot, mt);
                    }
                }
            }
            for (int i = 0; i < mt.bulletShots.size(); i++) {
                BulletShot bulletShot = mt.bulletShots.get(i);
                if (bulletShot.isLive) {
                    for (int j = 0; j < enemyTanks.size(); j++) {
                        EnemyTank enemyTank = enemyTanks.get(j);
                        hitTank(bulletShot, enemyTank);
                    }
                }
            }
            this.repaint();
//            if (!(mt.isLive)||enemyTanks.size() == 0) {
//                break;
//            }
            if (aePlayWave.isLive == false) {
                aePlayWave = new AePlayWave("src\\tankMusic.wav");
                aePlayWave.isLive = true;
                aePlayWave.start();
            }
            if (!mt.isLive) {
                winOrLose = 2;
                loop = false;
                System.out.println("游戏结束,你失败了");
            }
            if (enemyTanks.size() == 0) {
                winOrLose = 1;
                loop = false;
                System.out.println("游戏结束,你胜利了");
            }
        }
    }

    public void hitTank(BulletShot s, Tank tank) {
        switch (tank.getDirection()) {
            case 0:
            case 2:
                if (s.getX() >= tank.getX() && s.getX() <= tank.getX()
                        + 40 && s.getY() >= tank.getY() && s.getY() <= tank.getY() + 60) {
                    s.isLive = false;
                    tank.isLive = false;
                    new AePlayWave("src\\die.wav").start();
                    if (tank instanceof EnemyTank) {
                        enemyTanks.remove(tank);
                        Recorder.add();
                    }
                    bombs.add(new Bomb(tank.getX(), tank.getY()));
                }
                break;
            case 1:
            case 3:
                if (s.getX() >= tank.getX() && s.getX() <= tank.getX()
                        + 60 && s.getY() >= tank.getY() && s.getY() <= tank.getY() + 40) {
                    s.isLive = false;
                    tank.isLive = false;
                    new AePlayWave("src\\die.wav").start();
                    if (tank instanceof EnemyTank) {
                        enemyTanks.remove(tank);
                        Recorder.add();
                    }
                    bombs.add(new Bomb(tank.getX(), tank.getY()));
                }

        }
    }
}
